package Year21_Month10.day1017_2;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CardDemo {
    public static final String[] SUITS = {"♥","♠","♣","♦"};

    //定义一副牌
    public static List<Card> buyDeck(){
        List<Card> deck = new ArrayList<>(52);
        for(int i = 0;i < 4;i++){
            for(int j = 0;j < 13;j++){
                String suit = SUITS[i];
                int rank = j;
                Card card = new Card();
                card.rank = rank;
                card.suit = suit;
                deck.add(card);
            }
        }
        return deck;
    }

    //交换牌序
    public static void swap(List<Card> deck,int i,int j){
        Card c = deck.get(i);
        deck.set(i,deck.get(j));
        deck.set(j,c);
    }

    //打乱牌序----洗牌
    public static void shuffle(List<Card> deck){
        Random random = new Random();
        for(int i = deck.size() - 1;i > 0;i--){
            int r = random.nextInt(i);
            swap(deck,i,r);
        }
    }

    public static void main(String[] args) {
        List<Card> deck = buyDeck();
        System.out.println("刚买回来的牌");
        System.out.println(deck);
        shuffle(deck);
        System.out.println("打乱之后的牌");
        System.out.println(deck);

        //接着定义三个人，然后进行抓牌
        List<List<Card>> hands = new ArrayList<>();
        hands.add(new ArrayList<>());
        hands.add(new ArrayList<>());
        hands.add(new ArrayList<>());
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                //这里里面是3，外面是5，就是一共进行五轮，每个人每次拿一张牌
                hands.get(j).add(deck.remove(0));
            }
        }
        System.out.println("剩余的牌:");
        System.out.println(deck);
        System.out.println("A 手中的牌:");
        System.out.println(hands.get(0));
        System.out.println("B 手中的牌:");
        System.out.println(hands.get(1));
        System.out.println("C 手中的牌:");
        System.out.println(hands.get(2));

    }
}

class Card{
    int rank;//定义牌面值
    String suit;//定义花色

    @Override
    public String toString() {
        String s = "[" + suit + "," + rank + "]";
        return s;
    }
}
